<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="品牌信息" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="搜索关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入搜索关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格信息" prop="ruleId">
        <el-input
          v-model="queryParams.ruleId"
          placeholder="请输入规格信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0：添加商品信息 1：添加规格信息 2：添加描述信息" prop="step">
        <el-input
          v-model="queryParams.step"
          placeholder="请输入0：添加商品信息 1：添加规格信息 2：添加描述信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:Info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:Info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:Info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:Info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="InfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品描述" align="center" prop="productDesc">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title="商品描述"
            width="400"
            trigger="click"
          >
            <div v-html="scope.row.productDesc"></div>
            <el-button slot="reference" type="text">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="center" prop="type" />
      <el-table-column label="商品主图" align="center" prop="img">
        <template slot-scope="scope">
          <el-image :src="scope.row.img" style="width: 100px;height: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品轮播图" align="center" prop="carouselImages">
        <template slot-scope="scope">
          <el-image :src="scope.row.carouselImages" style="width: 100px;height: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="品牌信息" align="center" prop="brand" />
      <el-table-column label="商品状态" align="center" prop="status" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="规格信息" align="center" prop="ruleId" />
      <el-table-column label="0：添加商品信息 1：添加规格信息 2：添加描述信息" align="center" prop="step" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:Info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:Info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-steps :active="active" finish-status="success">
          <el-step title="添加商品信息"></el-step>
          <el-step title="添加规格信息"></el-step>
          <el-step title="添加描述信息"></el-step>
        </el-steps>

        <div v-show="active==0">
          <el-row :gutter="24">
            <el-col :span="12">
              <div class="grid-content bg-purple">
                <el-form-item label="商品名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入商品名称"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="grid-content bg-purple">
                <el-form-item label="单位" prop="unit">
                  <el-input v-model="form.unit" placeholder="请输入单位" />
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <div class="grid-content bg-purple">
                <el-form-item label="品牌信息" prop="brand">
                  <template>
                    <el-select v-model="form.brand" placeholder="请选择品牌信息">
                      <el-option
                        v-for="item in brandList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </template>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="grid-content bg-purple">
                <el-form-item label="分类" prop="type">
                  <el-cascader
                    v-model="form.type"
                    placeholder="请选择分类"
                    :options="categoryList"
                    :props="{ expandTrigger: 'hover', value: 'catId', label: 'name', children: 'childrenList' }"
                    @change="handleChange()">
                  </el-cascader>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <el-form-item label="商品主图" prop="img">
            <ImageUpload v-model="form.img"></ImageUpload>
          </el-form-item>
          <el-form-item label="商品轮播图" prop="carouselImages">
            <ImageUpload v-model="form.carouselImages"></ImageUpload>
          </el-form-item>
        </div>

        <div v-show="active==1">
          <el-form-item label="规格信息" prop="ruleId">
            <el-select value="ruleId" v-model="form.ruleId" placeholder="请选择规格信息">
              <el-option
              v-for="item in ruleInfoList"
               :key="item.id"
               :label="item.name"
               :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </div>

        <div v-show="active==2">
          <el-form-item label="商品描述" prop="productDesc">
            <el-input v-model="form.productDesc" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-top: 12px;" @click="pre" :disabled="active<1">上一步</el-button>
        <el-button style="margin-top: 12px;" @click="next" :disabled="active>=2">下一步</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/shop/Info";
import ImageUpload from "@/components/ImageUpload"
import {listBrandList} from "@/api/shop/brand"
import {listAll7} from "@/api/shop/shop"
import {listRuleInfo} from "@/api/shop/RuleInfo"
export default {
  name: "Info",
  components:{
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      InfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        productDesc: null,
        type: null,
        typeIds: null,
        img: null,
        carouselImages: null,
        commentCount: null,
        collectCount: null,
        brand: null,
        status: null,
        unit: null,
        keywords: null,
        ruleId: null,
        revision: null,
        step: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      stepMap:{
        0:'添加商品信息',
        1:'添加规格信息',
        2:'添加描述信息',
      },
      active:0,
      brandList:[],
      categoryList:[],
      ruleInfoList:[],
    };
  },
  created() {
    this.initListRuleInfo()
    this.initCategoryList();
    this.initBrandList();
    this.getList();
  },
  methods: {
    initListRuleInfo(){
      let params = {
        status: 0
      }
      listRuleInfo(params).then(res => {
        if (200 == res.code) {
          this.ruleInfoList = res.data.rows
        }
      })
    },
    initCategoryList() {
      listAll7().then(response => {
        if (200 == response.code) {
          this.categoryList = response.data
        }
      })
    },

    initBrandList() {
      let params = {
        status: 0
      }
      listBrandList(params).then(response => {
        if (200 == response.code) {
          this.brandList = response.data.rows
        }
      })
    },
    pre() {
      this.active--;
    },
    next() {
      this.active++;
    },
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        if (200 === response.code) {
          let infoList = response.data.rows;
          infoList.forEach(item => {
            item.step = this.switchStepByNum(item.step)
          })
          this.InfoList = infoList;
          this.total = response.data.total;
          this.loading = false;
        }
      });
    },
    switchStepByNum(num) {
      if (num == null || num == '' || num == undefined) {
        this.$modal.msgError("num is Null");
        return;
      }
      for (let stepMapKey in this.stepMap) {
        if (num == stepMapKey) {
          return this.stepMap[stepMapKey];
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.active = 0,
        this.form = {
          id: null,
          name: null,
          productDesc: null,
          type: null,
          typeIds: null,
          img: null,
          carouselImages: null,
          commentCount: null,
          collectCount: null,
          brand: null,
          status: null,
          unit: null,
          keywords: null,
          ruleId: null,
          revision: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          step: null
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        let data = response.data;
        this.active = data.step;
        this.form = data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/Info/export', {
        ...this.queryParams
      }, `Info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
